<?php
function result($nowdate,$nowjidu){
	global $db;
	if($db->pe_select("result",array('result_date'=>$nowdate."-".$nowjidu."-00"))){
	}else{
		$base = $db->pe_select("base",array('base_date'=>$nowdate."-00-00"));
		$lzf = $db->pe_select("lzf",array('lzf_date'=>$nowdate."-".$nowjidu."-00"));
		$jiduzsf = $db->pe_select("jiduzsf",array('jiduzsf_date'=>$nowdate."-".$nowjidu."-00"));
		$hgkf = $db->pe_select("hgkf",array('hgkf_date'=>$nowdate."-".$nowjidu."-00"));
		$yltskf = $db->pe_select("yltskf",array('yltskf_date'=>$nowdate."-".$nowjidu."-00"));
		$jlf = $db->pe_select("jlf",array('jlf_date'=>$nowdate."-".$nowjidu."-00"));
		if($base&&$lzf&&$jiduzsf&&$hgkf&&$yltskf&&$jlf){					
			$user_list = $db->pe_selectall("user");						
			foreach($user_list as $k=>$v){					
				$set['result_date'] = $nowdate."-".$nowjidu."-00";
				$set['result_user_code'] = $v['user_code'];
				$set['result_user_name'] = $v['user_name'];
				$set['result_user_company'] = $v['user_company'];
				$set['result_user_branch'] = $v['user_branch'];
				$set['result_user_job'] = $v['user_job'];
				$set['result_user_htcategory'] = $v['user_htcategory'];
				$ubase = $db->pe_select("base",array('base_date'=>$nowdate."-00-00",'base_user_code'=>$v['user_code']),'base_result');
				$ubase ? $set['result_base'] = $ubase['base_result'] : $set['result_base'] = 0;

				$ulzf = $db->pe_select("lzf",array('lzf_date'=>$nowdate."-".$nowjidu."-00",'lzf_user_code'=>$v['user_code']),'lzf_result');
				$ulzf ? $set['result_lzf'] = $ulzf['lzf_result'] : $set['result_lzf'] = 0;
				$ujiduzsf = $db->pe_select("jiduzsf",array('jiduzsf_date'=>$nowdate."-".$nowjidu."-00",'jiduzsf_user_code'=>$v['user_code']),'jiduzsf_result');
				$ujiduzsf ? $set['result_jiduzsf'] = $ujiduzsf['jiduzsf_result'] : $set['result_jiduzsf'] = 0;
				$uhgkf = $db->pe_select("hgkf",array('hgkf_date'=>$nowdate."-".$nowjidu."-00",'hgkf_user_code'=>$v['user_code']),'hgkf_result');
				$uhgkf ? $set['result_hgkf'] = $uhgkf['hgkf_result'] : $set['result_hgkf'] = 0;
				$uyltskf = $db->pe_select("yltskf",array('yltskf_date'=>$nowdate."-".$nowjidu."-00",'yltskf_user_code'=>$v['user_code']),'yltskf_result');
				$uyltskf ? $set['result_yltskf'] = $uyltskf['yltskf_result'] : $set['result_yltskf'] = 0;
				$ujlf = $db->pe_select("jlf",array('jlf_date'=>$nowdate."-".$nowjidu."-00",'jlf_user_code'=>$v['user_code']),'jlf_result');
				$ujlf ? $set['result_jlf'] = $ujlf['jlf_result'] : $set['result_jlf'] = 0;
				$set['result_all'] = $set['result_lzf'] + $set['result_jiduzsf'] + $set['result_jlf'] - $set['result_hgkf'] - $set['result_yltskf'];
				$db->pe_insert("result", $set);
			}
			//$jobresult = $db->sql_selectall("select result_user_job,result_all from bear_result where result_date = '{$nowdate}-{$nowjidu}-00' group by result_user_job");
			$job_list = $db->pe_selectall("job");
			$pjfarr = array();
			$jobnum = array();
			$joblandp = array();
			$jobl3 = array();
			$jobl2 = array();
			$jobl1 = array();
			$jobp3 = array();
			$jobp2 = array();
			$jobp1 = array();
			foreach($job_list as $v){
				$jobresult = $db->pe_selectall("result",array("result_date"=>$nowdate."-".$nowjidu."-00",'result_user_job'=>$v['job_name']),'result_all');
				$pjfarr[$v['job_name']] = 0;
				foreach($jobresult as $vv){
					$pjfarr[$v['job_name']] += $vv['result_all'];
				}
				if(count($jobresult)==0){
					$pjfarr[$v['job_name']] = 0;
				}else{
					$pjfarr[$v['job_name']] = round($pjfarr[$v['job_name']]/count($jobresult),2);
				}
				$jobnum[$v['job_name']] = $db->pe_num("result",array("result_date"=>$nowdate."-".$nowjidu."-00",'result_user_job'=>$v['job_name']));
				$level2 = explode("|", $v['job_level2']);
				$level1 = explode("|", $v['job_level1']);
				$joblandp[$v['job_name']] = array($v['job_level3'],$level2[0],$level2[1],$level1[0],$level1[1],$v['job_base'],$v['job_prize3'],$v['job_prize2'],$v['job_prize1']);
			}
			$resultsql .= " and result_date = '".$nowdate."-".$nowjidu."-00'";
			$resultsql .= " order by result_user_job desc,result_all desc";
			$result_list = $db->pe_selectall("result",$resultsql,'*');										
			$key = 1;
			foreach($result_list as $k=>$v){
				$setr['result_pjf'] = $pjfarr[$v['result_user_job']];
				
				$l3 = round($joblandp[$v['result_user_job']][0]*0.01*$jobnum[$v['result_user_job']],0);
				$l20 = round($joblandp[$v['result_user_job']][1]*0.01*$jobnum[$v['result_user_job']],0);
				$l21 = round($joblandp[$v['result_user_job']][2]*0.01*$jobnum[$v['result_user_job']],0);
				$l10 = round($joblandp[$v['result_user_job']][3]*0.01*$jobnum[$v['result_user_job']],0);
				$l11 = round($joblandp[$v['result_user_job']][4]*0.01*$jobnum[$v['result_user_job']],0);
				$basnum = $joblandp[$v['result_user_job']][5];
							
				if($k==0){
					$setr['result_top'] = 1;	
				}else{					
					if($v['result_user_job']==$result_list[$k-1]['result_user_job']){						
						$key++;
						$setr['result_top'] = $key;
					}else{
						$key = 1;
						$setr['result_top'] = $key;
					}					
				}	
				if($setr['result_top']<=$l3&&$v['result_base']>$basnum&&$v['result_all']>$setr['result_pjf']){
					$setr['result_result'] = "季度三星员工";
					$setr['result_prize'] = $joblandp[$v['result_user_job']][6];	
				}elseif($setr['result_top']>$l20&&$setr['result_top']<=$l21&&$v['result_base']>$basnum&&$v['result_all']>$setr['result_pjf']){
					$setr['result_result'] = "季度二星员工";	
					$setr['result_prize'] = $joblandp[$v['result_user_job']][7];
				}elseif($setr['result_top']>$l10&&$setr['result_top']<=$l11&&$v['result_base']>$basnum&&$v['result_all']>$setr['result_pjf']){
					$setr['result_result'] = "季度一星员工";	
					$setr['result_prize'] = $joblandp[$v['result_user_job']][8];
				}else{
					$setr['result_result'] = "";
					$setr['result_prize'] = 0;
				}				
				$db->pe_update("result",array("result_id"=>$v['result_id']),$setr);
			}
		}
	}
}
function reresult($nowdate,$nowjidu){
	global $db;
	$db->pe_delete("result",array('result_date'=>$nowdate."-".$nowjidu."-00"));	
	$base = $db->pe_select("base",array('base_date'=>$nowdate."-00-00"));
	$lzf = $db->pe_select("lzf",array('lzf_date'=>$nowdate."-".$nowjidu."-00"));
	$jiduzsf = $db->pe_select("jiduzsf",array('jiduzsf_date'=>$nowdate."-".$nowjidu."-00"));
	$hgkf = $db->pe_select("hgkf",array('hgkf_date'=>$nowdate."-".$nowjidu."-00"));
	$yltskf = $db->pe_select("yltskf",array('yltskf_date'=>$nowdate."-".$nowjidu."-00"));
	$jlf = $db->pe_select("jlf",array('jlf_date'=>$nowdate."-".$nowjidu."-00"));
	if($base&&$lzf&&$jiduzsf&&$hgkf&&$yltskf&&$jlf){					
		$user_list = $db->pe_selectall("user");						
		foreach($user_list as $k=>$v){					
			$set['result_date'] = $nowdate."-".$nowjidu."-00";
			$set['result_user_code'] = $v['user_code'];
			$set['result_user_name'] = $v['user_name'];
			$set['result_user_company'] = $v['user_company'];
			$set['result_user_branch'] = $v['user_branch'];
			$set['result_user_job'] = $v['user_job'];
			$set['result_user_htcategory'] = $v['user_htcategory'];
			$ubase = $db->pe_select("base",array('base_date'=>$nowdate."-00-00",'base_user_code'=>$v['user_code']),'base_result');
			$ubase ? $set['result_base'] = $ubase['base_result'] : $set['result_base'] = 0;

			$ulzf = $db->pe_select("lzf",array('lzf_date'=>$nowdate."-".$nowjidu."-00",'lzf_user_code'=>$v['user_code']),'lzf_result');
			$ulzf ? $set['result_lzf'] = $ulzf['lzf_result'] : $set['result_lzf'] = 0;
			$ujiduzsf = $db->pe_select("jiduzsf",array('jiduzsf_date'=>$nowdate."-".$nowjidu."-00",'jiduzsf_user_code'=>$v['user_code']),'jiduzsf_result');
			$ujiduzsf ? $set['result_jiduzsf'] = $ujiduzsf['jiduzsf_result'] : $set['result_jiduzsf'] = 0;
			$uhgkf = $db->pe_select("hgkf",array('hgkf_date'=>$nowdate."-".$nowjidu."-00",'hgkf_user_code'=>$v['user_code']),'hgkf_result');
			$uhgkf ? $set['result_hgkf'] = $uhgkf['hgkf_result'] : $set['result_hgkf'] = 0;
			$uyltskf = $db->pe_select("yltskf",array('yltskf_date'=>$nowdate."-".$nowjidu."-00",'yltskf_user_code'=>$v['user_code']),'yltskf_result');
			$uyltskf ? $set['result_yltskf'] = $uyltskf['yltskf_result'] : $set['result_yltskf'] = 0;
			$ujlf = $db->pe_select("jlf",array('jlf_date'=>$nowdate."-".$nowjidu."-00",'jlf_user_code'=>$v['user_code']),'jlf_result');
			$ujlf ? $set['result_jlf'] = $ujlf['jlf_result'] : $set['result_jlf'] = 0;
			$set['result_all'] = $set['result_lzf'] + $set['result_jiduzsf'] + $set['result_jlf'] - $set['result_hgkf'] - $set['result_yltskf'];
			$db->pe_insert("result", $set);
		}
		//$jobresult = $db->sql_selectall("select result_user_job,result_all from bear_result where result_date = '{$nowdate}-{$nowjidu}-00' group by result_user_job");
		$job_list = $db->pe_selectall("job");
		$pjfarr = array();
		$jobnum = array();
		$joblandp = array();
		$jobl3 = array();
		$jobl2 = array();
		$jobl1 = array();
		$jobp3 = array();
		$jobp2 = array();
		$jobp1 = array();
		foreach($job_list as $v){
			$jobresult = $db->pe_selectall("result",array("result_date"=>$nowdate."-".$nowjidu."-00",'result_user_job'=>$v['job_name']),'result_all');
			$pjfarr[$v['job_name']] = 0;
			foreach($jobresult as $vv){
				$pjfarr[$v['job_name']] += $vv['result_all'];
			}
			if(count($jobresult)==0){
				$pjfarr[$v['job_name']] = 0;
			}else{
				$pjfarr[$v['job_name']] = round($pjfarr[$v['job_name']]/count($jobresult),2);
			}
			$jobnum[$v['job_name']] = $db->pe_num("result",array("result_date"=>$nowdate."-".$nowjidu."-00",'result_user_job'=>$v['job_name']));
			$level2 = explode("|", $v['job_level2']);
			$level1 = explode("|", $v['job_level1']);
			$joblandp[$v['job_name']] = array($v['job_level3'],$level2[0],$level2[1],$level1[0],$level1[1],$v['job_base'],$v['job_prize3'],$v['job_prize2'],$v['job_prize1']);
		}
		$resultsql .= " and result_date = '".$nowdate."-".$nowjidu."-00'";
		$resultsql .= " order by result_user_job desc,result_all desc";
		$result_list = $db->pe_selectall("result",$resultsql,'*');										
		$key = 1;
		foreach($result_list as $k=>$v){
			$setr['result_pjf'] = $pjfarr[$v['result_user_job']];
			
			$l3 = round($joblandp[$v['result_user_job']][0]*0.01*$jobnum[$v['result_user_job']],0);
			$l20 = round($joblandp[$v['result_user_job']][1]*0.01*$jobnum[$v['result_user_job']],0);
			$l21 = round($joblandp[$v['result_user_job']][2]*0.01*$jobnum[$v['result_user_job']],0);
			$l10 = round($joblandp[$v['result_user_job']][3]*0.01*$jobnum[$v['result_user_job']],0);
			$l11 = round($joblandp[$v['result_user_job']][4]*0.01*$jobnum[$v['result_user_job']],0);
			$basnum = $joblandp[$v['result_user_job']][5];
						
			if($k==0){
				$setr['result_top'] = 1;	
			}else{					
				if($v['result_user_job']==$result_list[$k-1]['result_user_job']){						
					$key++;
					$setr['result_top'] = $key;
				}else{
					$key = 1;
					$setr['result_top'] = $key;
				}					
			}	
			if($setr['result_top']<=$l3&&$v['result_base']>$basnum&&$v['result_all']>$setr['result_pjf']){
				$setr['result_result'] = "季度三星员工";
				$setr['result_prize'] = $joblandp[$v['result_user_job']][6];	
			}elseif($setr['result_top']>$l20&&$setr['result_top']<=$l21&&$v['result_base']>$basnum&&$v['result_all']>$setr['result_pjf']){
				$setr['result_result'] = "季度二星员工";	
				$setr['result_prize'] = $joblandp[$v['result_user_job']][7];
			}elseif($setr['result_top']>$l10&&$setr['result_top']<=$l11&&$v['result_base']>$basnum&&$v['result_all']>$setr['result_pjf']){
				$setr['result_result'] = "季度一星员工";	
				$setr['result_prize'] = $joblandp[$v['result_user_job']][8];
			}else{
				$setr['result_result'] = "";
				$setr['result_prize'] = 0;
			}				
			$db->pe_update("result",array("result_id"=>$v['result_id']),$setr);
		}
		pe_success("计算成功！","?mod=result&year=".$nowdate."&jidu=".$nowjidu);
	}else{
		$monthnull = "";
		if(!$base){
			$monthnull .= $nowdate."年基础分&nbsp;";
		}
		if(!$lzf){
			$monthnull .= $nowdate."年第".$nowjidu."季度履职分&nbsp;";
		}
		if(!$jiduzsf){
			$monthnull .= $nowdate."年第".$nowjidu."季度业绩分&nbsp;";
		}
		if(!$hgkf){
			$monthnull .= $nowdate."年第".$nowjidu."季度合规扣分&nbsp;";
		}
		if(!$yltskf){
			$monthnull .= $nowdate."年第".$nowjidu."季度有理投诉扣分&nbsp;";
		}
		if(!$jlf){
			$monthnull .= $nowdate."年第".$nowjidu."季度奖励分&nbsp;";
		}
		pe_error("抱歉，".$monthnull."没有数据");
	}	
}
?>